import java.util.Arrays;

public class mySeqlist implements Ilist{
    public final int DEFALUT_CAPTICY=10;
    public int[] array;
    //定义起始数组大小为10
    public mySeqlist() {
        this.array =new int[DEFALUT_CAPTICY];
    }

    //顺序表的元素个数
    public int usesize;
    // 新增元素,默认在数组最后新增
    @Override
    public void add(int data) {
    //判断数组是否存储满了
        if(isFull()){
            //如果忙了就扩容
            grow();
        }
        array[usesize]=data;
        usesize++;
    }

    private int[] grow() {
        return this.array= Arrays.copyOf(this.array,2*this.array.length);
    }

    @Override
    public boolean isFull() {
        return this.usesize==array.length;
    }
    // 在 pos 位置新增元素
    @Override
    public void add(int pos, int data) {
        try{
            Check(pos);
            if(isFull()){
                //如果忙了就扩容
                grow();
            }
            for (int i = usesize; i >pos ; i--) {
                array[i]=array[i-1];
            }
            array[pos]=data;
            usesize++;
        }catch (posillegal e){
            e.printStackTrace();
        }
    }

    private void Check(int pos) {
        if(pos<0||pos>usesize){
            throw new posillegal("pos位置不合法!!!");
        }
    }
    // 判定是否包含某个元素
    @Override
    public boolean contains(int toFind) {
        //先判断数组是否为空
        if(isempty()){
            return false;
        }
       if(Find(toFind)){
           return true;
       }
       return false;
    }

    private boolean isempty() {
        return usesize==0;
    }

    private boolean Find(int tofind) {
        for (int i = 0; i < usesize; i++) {
            if(array[i]==tofind){
                return true;
            }
        }
        return false;
    }
   //获取值的下标
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usesize; i++) {
            if(array[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    //获取pos位置的值
    @Override
    public int get(int pos) {
       try{
           Check(pos);
           return array[pos];
       }catch (posillegal e){
           e.printStackTrace();
       }
       return  -1;
    }
   //把pos位置的元素设置变成value
    @Override
    public void set(int pos, int value) {
          array[pos]=value;
    }
   //去除某个值
    @Override
    public void remove(int toRemove) {
        if(isempty()){
            return;
        }
     int toremove=indexOf(toRemove);
        for (int i = toremove; i <usesize ; i++) {
            array[i]=array[i+1];
        }
        usesize--;
    }
    //求顺序表的长度
    @Override
    public int size() {
        return usesize;
    }
     //清空顺序表
    @Override
    public void clear() {
          usesize=0;
    }
   //打印顺序表
    @Override
    public void display() {
        for (int i = 0; i < usesize; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
